public class Sort2 {
    // 插入排序：O(n^2)
    public static void insertSort(int[] array) {
        // write code  here
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    // 希尔排序：O(n^1.3)
    public static void shellSort(int[] array) {
        // write code  here
        int gap = array.length;
        gap /= 2;
        while (gap >= 1) {
            shell(array, gap);
            gap /= 2;
        }
    }

    private static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    // 选择排序：O(n^2)
    public static void selectSort(int[] array) {
        // write code  here
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            boolean flag = false;
            for (int j = i + 1; j < array.length; j++) {
                if (array[minIndex] > array[j]) {
                    minIndex = j;
                    flag = true;
                }
            }
            if (flag) {
                int tmp = array[i];
                array[i] = array[minIndex];
                array[minIndex] = tmp;
            }
        }
    }

    // 堆排序：O(nlogn)
    public static void heapSort(int[] array) {
        // write code  here
        for (int parent = (array.length - 1 - 1) >> 1; parent >= 0; parent--) {
            shiftDown(array, parent, array.length);
        }
        int useSize = array.length;
        while (useSize > 0) {
            swap(array, 0, useSize - 1);
            useSize--;
            shiftDown(array, 0, useSize);
        }
    }

    //向下调整
    public static void shiftDown(int[] array, int parent, int len) {
        int child = parent << 1 + 1;
        while (child < array.length) {
            if (child + 1 < array.length && array[child] < array[child + 1]) {
                child++;
            }
            if (array[child] > array[parent]) {
                swap(array, parent, len);
                parent = child;
                child = parent << 1 + 1;
            } else {
                break;
            }
        }
    }

    //交换
    public static void swap(int[] a, int i, int j) {
        int tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }

    // 冒泡排序
    public static void bubbleSort(int[] array) {
        // write code  here
        for (int i = 0; i < array.length - 1; i++) {
            boolean flag = true;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j + 1] < array[j]) {
                    swap(array, j + 1, j);
                    flag = false;
                }
            }
            if (flag) break;
        }
    }

    // 快速
    public static void quickSort(int[] array) {
        // write code  here
        quick(array, 0, array.length - 1);
    }

    //左闭右闭区间
    private static void quick(int[] array, int left, int right) {
        if (left >= right) return;
//        if (left - right + 1 < 8) {
//            insertSort2(array, left, right + 1);//左闭右开区间
//        }
        int mid = midTreeNum(array, left, right);
        swap(array, mid, left);
        int qMid = partion(array, left, right);
        quick(array, left, qMid - 1);
        quick(array, qMid + 1, right);
    }

    private static int partion(int[] array, int start, int end) {
        int tmp = array[start];
        while (start < end) {
            while (start < end && array[end] >= tmp)
                end--;
            array[start] = array[end];
            while (start < end && array[start] <= tmp)
                start++;
            array[end] = array[start];
        }
        array[start] = tmp;
        return start;
    }

    // 插入排序：O(n^2)
    public static void insertSort2(int[] array, int start, int end) {
        // write code  here
        for (int i = start + 1; i < end; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= start; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    //三数取中法
    private static int midTreeNum(int[] array, int left, int right) {
        int mid = left + (right - left) / 2;
        if (array[left] > array[right]) {
            if (array[mid] > array[left]) return left;
            else if (array[mid] < array[right]) return right;
            else return mid;
        } else {
            if (array[mid] > array[right]) return right;
            else if (array[mid] < array[left]) return left;
            else return mid;
        }
    }

    // 归并排序---递归
    public static void mergeSort(int[] array) {
        // write code  here
        mergeDiver(array, 0, array.length - 1);
    }

    private static void mergeDiver(int[] array, int left, int right) {
        if (left >= right) return;
        int mid = left + (right - left) / 2;
        mergeDiver(array, left, mid);
        mergeDiver(array, mid + 1, right);
        merge(array, left, right, mid);
    }

    private static void merge(int[] array, int left, int right, int mid) {
        int s1 = left;
        int s2 = mid + 1;
        int[] tmp = new int[right - left + 1];
        int k = 0;
        while (s1 <= mid && s2 <= right) {
            if (array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            } else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= mid) {
            tmp[k++] = array[s1++];
        }
        while (s2 <= right) {
            tmp[k++] = array[s2++];
        }
        for (int i = 0; i < tmp.length; i++) {
            array[i + left] = tmp[i];
        }
    }

    // 归并排序---非递归
    public static void mergeSort2(int[] array) {
        // write code  here
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i += 2 * gap) {
                int left = i;
                int mid = left + gap - 1;
                if (mid >= array.length)
                    mid = array.length - 1;
                int right = mid + gap;
                if (right >= array.length)
                    right = array.length - 1;
                merge(array, left, right, mid);
            }
            gap *= 2;
        }
    }

    // 计数排序
    public static void countSort(int[] array) {
        // write code  here
        int max = array[0];
        int min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (max < array[i]) max = array[i];
            if (min > array[i]) min = array[i];
        }
        int[] count = new int[max - min + 1];
        for (int i = 0; i < array.length; i++)
            count[array[i] - min]++;
        int k = 0;
        for (int i = 0; i < count.length; i++) {
            while (count[i] != 0) {
                array[k] = i + min;
                count[i]--;
                k++;
            }
        }
    }

    public static void main(String[] args) {
        int[] a = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
        countSort(a);
        for (int a1 : a) {
            System.out.println(a1);
        }
    }
}
